home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Reference Guide
/
C-C++ Interactive Reference Guide.iso
/
c_ref
/
csource5
/
376_01
/
os2tool.000
/
ALARM.C
next >
Wrap
C/C++ Source or Header
|
1992-08-27
|
12KB
|
449 lines
/*
* ALARM.C - Show a alarm message at a specified time.
*
*
* PROGRAMMER: Martti Ylikoski
* CREATED: 16.3.1992
*/
static char *VERSION="Version 1.0. Copyright(c) Martti Ylikoski, 1992. " ;
/*
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "list.h"
#include <param.h>
#include <paramstd.h>
ParamEntry pentry[12] = {
"P", &ParamSetPause, 0,
"F", &ParamSetFold, 0,
"V", &ParamSetVerbose, 0,
"R", &ParamSetReport, 0,
"S", &ParamSetSubDirs, 0,
"?", &ParamSetHelp, 1,
"H", &ParamSetHelp, 1,
"NOD", &ParamSetNoDefault, 1,
"TEST", &ParamSetTest, 0,
"Y", &ParamSetYes, 0,
"N", &ParamSetTest, 0,
"\0", NULL, 0
} ;
ParamBlock params = {
"/-", IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
pentry
} ;
extern unsigned long pflags ; /* needed because of paramstd */
/* from wndbox.h */
#define SNGL_TOPLEFT 0
#define SNGL_TOPRIGHT 1
#define SNGL_LINE 2
#define SNGL_BAR 3
#define SNGL_BOTLEFT 4
#define SNGL_BOTRIGHT 5
char sngl[6] = {
218,
191,
196,
179,
192,
217
} ;
#define INCL_VIO
#define INCL_DOS
#define INCL_KBD
#include <os2.h>
static char *progname ;
static char *deffile = ".\\alarm.dat" ;
/* local prototypes */
static void DrawBox(int TopRow, int TopCol, int BotRow, int BotCol) ;
static void DrawData(HNDLIST *lhandle, int TopRow, int TopCol, int BotRow, int BotCol) ;
static void AddToDisplay(HNDLIST *lhandle, char *fbuff, int *DataLineCount, int *MaxLineLen) ;
int StrExpandTime(char *str, char *tsepa, UCHAR *hours, UCHAR *minutes, UCHAR *seconds) ;
void TimeWaitUntil(int hours, int minutes, int seconds) ;
void AlarmShow(HNDLIST *lhandle, int MaxLineLen, int DataLineCount) ;
void MessageExpand(HNDLIST *lhandle, char *tmp2, int *MaxLineLen, int *DataLineCount ) ;
int main(int argc, char *argv[])
{
int MaxLineLen, DataLineCount, c ;
FILE *fp ;
HNDLIST *lhandle ;
char fbuff[125], *tmp, *tmp2 ;
UCHAR hours, minutes, seconds ;
progname = argv[0] ;
ParamHandle(¶ms, &argc, argv) ;
if ((lhandle = ListInit()) == NULL)
{
printf("Error in ListInit\nExit\n") ;
return( 1 ) ;
}
DataLineCount = 0 ;
MaxLineLen = 0 ;
if (pflags & PA_HELP)
{
sprintf(fbuff,"%s - Show a alarm message at a specified time to the user.\n", progname) ;
AddToDisplay(lhandle, fbuff, &DataLineCount, &MaxLineLen) ;
AddToDisplay(lhandle,VERSION, &DataLineCount, &MaxLineLen) ;
AddToDisplay(lhandle,"Usage: alarm [time message] | [/F alarmfile] | [/? | /H] [/NOD]", &DataLineCount, &MaxLineLen) ;
AddToDisplay(lhandle,"Where,", &DataLineCount, &MaxLineLen) ;
AddToDisplay(lhandle," message = Displayed message with \\n between different lines.", &DataLineCount, &MaxLineLen) ;
AddToDisplay(lhandle," /F alarmfile = Alarm times and messages in a file", &DataLineCount, &MaxLineLen) ;
AddToDisplay(lhandle," /NOD = No default. Automatically added two last lines are not displayed.", &DataLineCount, &MaxLineLen) ;
AddToDisplay(lhandle," /H,/? = Help", &DataLineCount, &MaxLineLen) ;
AlarmShow(lhandle, MaxLineLen, DataLineCount) ;
return( 0 ) ;
}
if (argc == 3 && (strcmpi(argv[1], "-F") != 0 && strcmpi(argv[1], "/f") !=0) )
{
StrExpandTime(argv[1], ":", &hours, &minutes, &seconds) ;
MessageExpand(lhandle, argv[2], &MaxLineLen, &DataLineCount ) ;
TimeWaitUntil(hours, minutes, seconds) ;
}
else
{
if (argc >2 && ( strcmpi(argv[1], "/f") == 0 || strcmpi(argv[1], "-f")==0))
deffile = argv[2] ;
if ((fp = fopen (deffile, "r")) == NULL)
{
sprintf(fbuff,"%s : error opening messagefile %s.", progname, deffile) ;
AddToDisplay(lhandle, fbuff, &DataLineCount, &MaxLineLen) ;
AddToDisplay(lhandle, " ", &DataLineCount, &MaxLineLen) ;
}
else
{
while (fgets(fbuff, sizeof(fbuff), fp ) != NULL)
{
char t[11] ;
if (strlen(fbuff) == 0 || fbuff[0] == '\n')
continue ;
if (fbuff[0] == '#' || fbuff[0] == ';') /* handle comment lines */
continue ;
if (strnicmp(fbuff, "CONTINUE",8) == 0)
{
rewind(fp) ;
continue ;
}
if (strnicmp(fbuff, "BREAK",5) == 0 || strnicmp(fbuff, "QUIT",4) == 0)
{
fclose(fp) ;
return( 0 ) ;
}
tmp = strtok(fbuff, " ") ;
strcpy(t, tmp) ;
StrExpandTime(t, ":", &hours, &minutes, &seconds) ;
tmp += strlen(tmp)+1 ;
MessageExpand(lhandle, tmp, &MaxLineLen, &DataLineCount ) ;
TimeWaitUntil(hours, minutes, seconds) ;
AlarmShow(lhandle, MaxLineLen, DataLineCount) ;
DataLineCount = 0 ;
MaxLineLen = 0 ;
ListExit(lhandle) ; /* missing function ListClear */
if ((lhandle = ListInit()) == NULL)
{
printf("Error in ListInit\nExit\n") ;
return( 1 ) ;
}
}
fclose (fp) ;
return( 0 ) ;
}
}
AlarmShow(lhandle, MaxLineLen, DataLineCount) ;
return( 0 ) ;
}
void MessageExpand(HNDLIST *lhandle, char *tmp2, int *MaxLineLen, int *DataLineCount )
{
char *tmp ;
// tmp2 = argv[2] ;
while ( 1 )
{
if ( (tmp =strchr(tmp2, (int) '\\')) != NULL && tmp[1] == 'n' )
{
tmp[0] = '\0' ;
tmp[1] = '\0' ;
*DataLineCount += 1 ;
// if (fbuff[strlen(tmp2)-1] == '\n')
// fbuff[strlen(tmp2)-1] = '\0' ; /* remove trailing newline */
if (strlen(tmp2) > *MaxLineLen)
*MaxLineLen = strlen(tmp2) ;
ListAdd(lhandle, tmp2, strlen(tmp2)+1) ; /* +1 because we store also \0 */
tmp2 = tmp+2 ;
if (tmp2[0] == '\0')
break ;
}
else
if (tmp2[0] == '\0' || tmp2[0] == '\n')
break ;
else
{
*DataLineCount += 1 ;
if (strlen(tmp2) > *MaxLineLen)
*MaxLineLen = strlen(tmp2) ;
ListAdd(lhandle, tmp2, strlen(tmp2)+1) ; /* +1 because we store also \0 */
break ;
}
}
}
void AlarmShow(HNDLIST *lhandle, int MaxLineLen, int DataLineCount)
{
USHORT fWait = VP_WAIT | VP_OPAQUE ;
int TopRow, TopCol, BotRow, BotCol, MaxCol, MaxRow;
KBDKEYINFO kbdi ;
char fbuff[125] ;
VioPopUp(&fWait, 0) ;
MaxCol = 79 ;
MaxRow = 24 ;
if (pflags & PA_NODEFAULT)
;
else
{
if (MaxLineLen <28)
MaxLineLen = 28 ;
memset(fbuff, sngl[SNGL_LINE], (size_t) MaxLineLen) ;
ListAdd(lhandle, fbuff, strlen(fbuff)+1) ; /* +1 because we store also \0 */
strcpy(fbuff, "Press ESC to cancel message.") ;
ListAdd(lhandle, fbuff, strlen(fbuff)+1) ; /* +1 because we store also \0 */
if (strlen(fbuff) > MaxLineLen)
MaxLineLen = strlen(fbuff) ;
DataLineCount += 2 ;
}
TopRow = (MaxRow - DataLineCount)/2 -1 ;
if (TopRow <0)
TopRow = 0 ;
TopCol = (MaxCol - MaxLineLen)/2 - 1 ;
if (TopCol <0)
TopCol = 0 ;
BotRow = 24 -(MaxRow - DataLineCount)/2 +1 ;
BotCol = 79 - (MaxCol - MaxLineLen)/2 + 1 ;
DrawBox(TopRow, TopCol, BotRow, BotCol) ;
DrawData(lhandle, TopRow, TopCol, BotRow, BotCol) ;
do
{
KbdCharIn(&kbdi, IO_WAIT, 0) ;
} while (kbdi.chChar != 27) ;
VioEndPopUp( 0 ) ;
}
static void DrawBox(int TopRow, int TopCol, int BotRow, int BotCol)
{
int i ;
BYTE WndCol = 0x17 ;
VioWrtCharStrAtt(&sngl[SNGL_TOPLEFT],1,TopRow, TopCol, &WndCol, 0) ;
for (i = 1 ; i < BotCol - TopCol ; i++)
VioWrtCharStrAtt(&sngl[SNGL_LINE],1,TopRow, TopCol+i, &WndCol, 0) ;
VioWrtCharStrAtt(&sngl[SNGL_TOPRIGHT],1,TopRow, BotCol, &WndCol, 0) ;
for (i = 1 ; i < BotRow - TopRow; i++)
{
VioWrtCharStrAtt(&sngl[SNGL_BAR],1, TopRow+i, TopCol, &WndCol, 0) ;
VioWrtCharStrAtt(&sngl[SNGL_BAR],1, TopRow+i, BotCol, &WndCol, 0) ;
}
VioWrtCharStrAtt( &sngl[SNGL_BOTLEFT], 1, BotRow, TopCol, &WndCol, 0) ;
for (i = 1 ; i < BotCol - TopCol ; i++)
VioWrtCharStrAtt(&sngl[SNGL_LINE],1,BotRow, TopCol+i, &WndCol, 0) ;
VioWrtCharStrAtt(&sngl[SNGL_BOTRIGHT],1,BotRow, BotCol, &WndCol, 0) ;
}
static void DrawData(HNDLIST *lhandle, int TopRow, int TopCol, int BotRow, int BotCol)
{
unsigned short width, height, drawlen ;
LIST_ENTRY *templist ;
long templ, templ2 ;
int actioncode ;
char *buf ;
unsigned long buflen ;
int i ;
unsigned char attrib ;
unsigned char cell[2] ;
cell[0] = ' ' ;
cell[1] = 0x17; // wnd->WndCol ;
height = BotRow - TopRow ; /* calculate the height of the window */
width = BotCol - TopCol ; /* calculate the width of the window */
attrib = 0x17 ; //WndCol ;
ListFirst(lhandle) ;
/* Note different drawing limits for windows with/without borders */
for (i = 1 ; i <= height - 1 ; i++)
{
ListReturn(lhandle, &buf, &buflen) ;
VioWrtCharStrAtt( buf, buflen , TopRow+i, TopCol+1, &attrib, 0) ;
VioWrtNCell(cell, width-1 - buflen, TopRow+i, TopCol+1+buflen, 0) ;
if ( ListNext(lhandle) != LIST_OK)
break ;
}
/* Note different drawing limits for windows with/without borders */
for ( ++i ; i <= height - 1 ; i++)
{
VioWrtNCell(cell, width-1 , TopRow+i, TopCol+1, 0) ;
}
}
static void AddToDisplay(HNDLIST *lhandle, char *fbuff, int *DataLineCount, int *MaxLineLen)
{
*DataLineCount += 1 ;
if (fbuff[strlen(fbuff)-1] == '\n')
fbuff[strlen(fbuff)-1] = '\0' ; /* remove trailing newline */
if (strlen(fbuff) > *MaxLineLen)
*MaxLineLen = strlen(fbuff) ;
ListAdd(lhandle, fbuff, strlen(fbuff)+1) ; /* +1 because we store also \0 */
}
void TimeWaitUntil(int hours, int minutes, int seconds)
{
DATETIME dt ;
LONG sleepval ;
USHORT tomorrow = FALSE ;
DosGetDateTime(&dt) ;
if (dt.hours > hours) /* schedule for tomorrow */
tomorrow = TRUE ;
do
{
DosGetDateTime(&dt) ;
if(dt.hours == 0)
tomorrow = FALSE ;
sleepval = ((hours - dt.hours)*3600 + (minutes-dt.minutes)*60 +
seconds-dt.seconds)/2 ;
if (sleepval < 0 && tomorrow == FALSE)
break ;
if (sleepval == 0)
DosSleep(320L) ;
else
DosSleep(sleepval*1000L) ;
} while ( hours > dt.hours ||
(hours ==dt.hours && minutes > dt.minutes) ||
(hours ==dt.hours && minutes == dt.minutes && seconds > dt.seconds) ||
tomorrow == TRUE ) ;
}
int StrExpandTime(char *str, char *tsepa, UCHAR *hours, UCHAR *minutes, UCHAR *seconds)
{
int add = FALSE ;
DATETIME dt ;
char *tmp ;
/* set default values for time */
DosGetDateTime(&dt) ;
*hours = dt.hours ;
*minutes = dt.minutes ;
*seconds = dt.seconds ;
if (str[0] == '+')
add = TRUE ;
if ( (tmp=strtok(str, tsepa)) == NULL)
return( 1 ) ;
if (add==TRUE)
*hours += atoi(tmp) ;
else
*hours=atoi(tmp) ;
if (*hours >24)
{
*hours -= 24 ;
}
/* new default values */
if (add == FALSE)
{
*minutes = '0' ;
*seconds = '0' ;
}
if ( (tmp=strtok(NULL, tsepa)) == NULL)
return( 0 ) ;
if (add==TRUE)
*minutes += atoi(tmp) ;
else
*minutes = atoi(tmp) ;
if (*minutes >60)
{
*minutes -= 60 ;
*hours += 1 ;
}
if ( (tmp=strtok(NULL, tsepa)) == NULL)
return( 0 ) ;
if (add==TRUE)
*seconds += atoi(tmp) ;
else
*seconds = atoi(tmp) ;
if (*seconds >60)
{
*seconds -=60 ;
*minutes += 1 ;
}
return( 0 ) ;
}